import { Toast } from 'antd-mobile';
import axios from 'axios'

const instance = axios.create({
    baseURL: '/api',
    timeout: 40000,
    headers: {'X-Custom-Header': 'foobar'}
  });

  // 添加请求拦截器
  instance.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    Toast.show({
        icon: 'loading加载',
        // content: <CountDownText />,
        duration: 0,
      })
    const token=localStorage.getItem('token')
    if(token){
        config.headers.Authorization=`Bearer ${token}`
    }
    return config;
  }, function (error) {
    // 对请求错误做些什
    // 么
    Toast.clear()

    return Promise.reject(error);
  });

// 添加响应拦截器
instance.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    Toast.clear()

    return response;
  }, function (error) {
    // 对响应错误做点什么
    Toast.clear()

    if (error.response) {
        const status=error.response.data
        switch (status){
            case 200:
                console.log('请求成功');
            break;
            case 303:
                console.log('重定向');
            break;
            case 401:
                console.log('路径错误');
            break;
            case 500:
                console.log('浏览器报错');
            break;
        }
      } else if (error.request) {
        // The request was made but no response was received
        // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
        // http.ClientRequest in node.js
        console.log(error.request);
      } else {
        // Something happened in setting up the request that triggered an Error
        console.log('Error', error.message);
      }
    return Promise.reject(error);
  });

export default instance




